package com.zuikaku.controller;

import com.alibaba.fastjson.JSONObject;
import com.zuikaku.pojo.TempUser;
import com.zuikaku.service.INumberCalculateService;
import com.zuikaku.utils.AliyunSMS;
import com.zuikaku.utils.SMTPEmail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Date;

@Controller
@RequestMapping("/test")
@SessionAttributes("activeUser")
public class TestController {

    @Resource
    private INumberCalculateService numberCalculateService;

    @Autowired
    private AliyunSMS aliyunSMS;

    @GetMapping("/add/{num1}/{num2}")
    public String addTwoNumber(@PathVariable("num1")Integer num1, @PathVariable("num2")Integer num2, Model model){
        int res = numberCalculateService.add2Number(num1,num2);
        model.addAttribute("num1",num1);
        model.addAttribute("num2",num2);
        model.addAttribute("res",res);

        return "/sum";
    }

    @RequestMapping(value = "/getInput",method = RequestMethod.GET)
    @ResponseBody
    public String getInput(@RequestParam(value = "name",required = false,defaultValue = "zhangsan")String name){
        System.out.println(name);
        return name;
    }

    @GetMapping("/add2/{num1}/{num2}")
    public ModelAndView addTwoNumber2(@PathVariable("num1")Integer num1, @PathVariable("num2")Integer num2) {
        int res = numberCalculateService.add2Number(num1, num2);
        ModelAndView mv =new ModelAndView("/sum");
        mv.addObject("num1", num1);
        mv.addObject("num2", num2);
        mv.addObject("res", res);
        return mv;
    }


    @GetMapping("/getLoginUser")
    @ResponseBody
    public TempUser getLoginUser(@SessionAttribute("activeUser")TempUser user){
        System.out.println(user);
        return user;

    }

    @PostMapping("/login")
    @ResponseBody
    public String login(TempUser user, Model model){
        model.addAttribute("activeUser",user);
        return "ok";
    }

    @GetMapping("/getCookie")
    @ResponseBody
    public String getCookie(@CookieValue("address")String address){
        System.out.println("address:"+address);
        return address;

    }

    @GetMapping("/setCookie")
    @ResponseBody
    public String setCookie(HttpServletResponse response){
        response.addCookie(new Cookie("address","上海杨浦江湾镇"));
        return "ok";
    }

    //todo ajax篇章讲解
    public String getFromApplicationJson(@RequestBody TempUser tempUser){
        return null;
    }

    //todo SpringBoot案例实战时使用
    public String getDate(@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:sss") Date inputDate){
        return null;
    }

    //todo SpringMVC文件上传篇章
    public String test(MultipartFile file){
        return "";
    }


    @ResponseBody
    @GetMapping("/sendSMS")
    public String sendSMS(){
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("time","2023");
        aliyunSMS.send("13761513572",jsonObject.toJSONString(),
                "SMS_225910027");

        return "ok";
    }


    @ResponseBody
    @GetMapping("/sendEmail")
    public String sendEmail(){
        return String.valueOf(SMTPEmail.sendEmail("tanyu159@live.com","测试测试测hi是","<h1>测试正文</h1>",null));
    }
}
